// pages/refund/refund.js
const app = getApp()
var api = require('../../../utils/api.js')
var util = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    num: 1,
    goods: [],
		description: '',
		userName: '',
		phone: '',
    refundBg: true,
    refundText: '请选择',
    refundBox: [{
        'name': '我不想要了',
        'option': false
      },
      {
        'name': '买错了',
        'option': false
      },
      {
        'name': '质量问题',
        'option': false
      },
      {
        'name': '其他',
        'option': false
      }
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
			oid: (options.oid) ? options.oid : '',
			op_id: (options.op_id) ? options.op_id : '',
			type: (options.type) ? options.type : '',
			id: (options.id) ? options.id:''
    })
		this.returnOrderDetails()
  },


  onShow: function() {
    this.orderDetails();
  },

	returnOrderDetails: function () {
		var that = this;
		var id = this.data.id;
		if(id){
			api.POST({
				params: { "id": id },
				url: app.globalData.base_url + 'aftersale/getInfo',
				success: function (res) {
					if (util.isEmpty(res.data.code)) {
						var productDetails = res.data.data;
						that.setData({
							refundText: productDetails.after_reason,
							num: productDetails.number,
							description: productDetails.describe,
							goods: productDetails.img ? productDetails.img:[],
							userName: productDetails.username,
							phone: productDetails.mobile,
						})
					}
				}
			})
		}
	},

  orderDetails: function() {
    var that = this;
    var oid = this.data.oid;
    var op_id = this.data.op_id;
    api.POST({
      params: {
        "order_id": oid,
        'op_id': op_id
      },
      url: app.globalData.base_url + 'aftersale/getGoodsInfo',
      success: function(res) {
        if (util.isEmpty(res.data.code)) {
          var productDetails = res.data.data;
          var total = (productDetails.number * productDetails.price).toFixed(2);
          var returnPrice = (productDetails.price * that.data.num).toFixed(2);
          that.setData({
            productDetails: productDetails,
            total: total,
            returnPrice: returnPrice
          })
        }
      }
    })
  },
  addNum: function() {
    var maxNum = this.data.productDetails.number;
    var num = this.data.num;
    var price = this.data.productDetails.price;
    if (num < maxNum) {
      num++
    }
    var returnPrice = (price * num).toFixed(2)
    this.setData({
      num: num,
      returnPrice: returnPrice
    })
  },
  minusNum: function() {
    var num = this.data.num;
    var price = this.data.productDetails.price;
    if (num > 1) {
      num--
    }
    var returnPrice = (price * num).toFixed(2)
    this.setData({
      num: num,
      returnPrice: returnPrice
    })
  },

  //添加问题图片
  editGoodsImgEvent: function() {
    var that = this;
    wx.chooseImage({
      count: 9 - that.data.goods.length,
      sizeType: ['compressed'],
      success: function(res) {
        var tempFilePaths = res.tempFilePaths;

        for (var i = 0; i < tempFilePaths.length; i++) {
          wx.uploadFile({
            url: app.globalData.base_url + "img/saveImage",
            filePath: tempFilePaths[i],
            name: 'file',
            success: function(res) {
              var data = JSON.parse(res.data);
              that.data.goods.push(data.data.url);
              that.setData({
                goods: that.data.goods
              });
            }
          })
        }
      },
    })
  },
  //删除问题图片
  closeImages: function(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var goods = this.data.goods;
    goods.splice(index, 1);
    this.setData({
      goods: goods
    })
  },

  description: function(e) {
    var value = e.detail.value
    this.setData({
      description: value
    })
  },

  userNameCol: function(e) {
    var value = e.detail.value
    this.setData({
      userName: value
    })
  },

  phoneCol: function(e) {
    var value = e.detail.value
    this.setData({
      phone: value
    })
  },

  clickRefund: function(e) {
    var index = e.currentTarget.dataset.index
    this.setData({
			refundText: this.data.refundBox[index].name
    })
  },

  //提交申请
  submit: function() {
    var order_id = this.data.oid;
    var op_id = this.data.op_id;
    var munber = this.data.num;
    var describe = this.data.description;
    var img = this.data.goods;
    var username = this.data.userName;
    var mobile = this.data.phone;
    var type = this.data.type;
    var id = this.data.id;
    var after_reason = this.data.refundText;

    if (after_reason == '请选择') {
      util.showError("请选择退货原因");
      return;
    }

    if (!username) {
      util.showError("请输入您的姓名");
      return;
    }

    if (!util.isEmpty(mobile)) {
      if (!util.validateMobile(mobile)) {
        util.showStatus("请输入正确的手机号");
        return false
      }
    } else {
      util.showStatus("请输入正确的手机号");
      return false
    }

    if (img.length < 1 && this.data.productDetails.order_status == '5') {
      util.showError("请上传图片~");
      return;
    }
    var params = new Object()
    var that = this;
    params.order_id = order_id;
    params.op_id = op_id;
    params.id = id;
    params.mobile = mobile;
    params.number = munber;
    params.username = username;
    params.describe = describe;
    params.type = type;
    params.after_reason = after_reason;
    params.img = img;
    api.POST({
      params: params,
      url: app.globalData.base_url + 'aftersale/apply',
      success: function(res) {
        if (util.isEmpty(res.data.code)) {
					util.showError("提交成功");
					setTimeout(function(){
						wx.redirectTo({
							url: '/center/pages/refundCheck/refundCheck?id=' + res.data.data.id
						})
					},1500)
        }else{
          util.showError(res.data.message);
        }
      },
      fail: function(fail) {
        util.showError("../../../", "获取失败，请检查网络");
      }
    })

  },

  refundSel: function() {
    this.setData({
      refundBg: !this.data.refundBg
    })
    console.log(this.data.refundBg)
  }

})